package com.zhou.controller;

import com.zhou.entity.WareHouse;
import com.zhou.service.WareHouseService;
import com.zhou.utis.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库操作
 *
 * @author zhouyuyan
 */
@RestController
@RequestMapping("ware_house")
@Component //将这个类交给Spring托管
public class WareHouseController {
    @Autowired
    private WareHouseService wareHouseService;

    /**
     * 仓库库存查询
     */
    @GetMapping("page")
    public Result getAll(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer pageSize,
                         @RequestParam(required = false) String expireTime) {
        try {
            List<WareHouse> all = wareHouseService.getAll(page, pageSize, expireTime);
            return Result.success(all);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 仓库的过期时间修改
     */
    @PutMapping("update")
    public Result update(String expireTime, String wareHouseId) {
        try {
            Boolean byTime = wareHouseService.getByTime(expireTime, wareHouseId);
            return Result.success(byTime);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 仓库的过期时间查询
     */
    @GetMapping("get_by_time")
    @Scheduled(cron = "0 0 10 * * ?") //每天的早上十点请求一次方法
    public Result getByTime() {
        try {
            List<WareHouse> timeAll = wareHouseService.getTimeAll();
            return Result.success(timeAll);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
}
